/*
 * WordCount_srv.c
 *
 *  Created on: Mar 26, 2012
 *      Author: root
 */
#include	"unp.h"
#include    <string.h>

int main(int argc, char **argv) {
	int listenfd, connfd;
	pid_t childpid;
	socklen_t clilen;
	struct sockaddr_in cliaddr, servaddr;
	void sig_chld(int);

	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(SERV_PORT);

	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	Signal(SIGCHLD, sig_chld); /* must call waitpid() */

	for (;;) {
		clilen = sizeof(cliaddr);
		if ((connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) < 0) {
			if (errno == EINTR)
				continue; /* back to for() */
			else
				err_sys("accept error");
		}

		if ((childpid = Fork()) == 0) { /* child process */
			Close(listenfd); /* close listening socket */

			ssize_t n;
			char buf[MAXLINE];
			char byteCount[MAXLINE];
			char wordCount[MAXLINE];
			char sendbuf[MAXLINE];
			int intCount = 0;
			char seps[] = "' '\t\n";                  //for word count
			char *token;                              //for word count

			char stop[] = ".\n";                      //for end signal
			int totalbyte=0,totalword=0,totalline=0;  //for each line count
			char totalByte[MAXLINE];                  //total Byte count
			char totalWord[MAXLINE];                  //total Word count
			char totalLine[MAXLINE];                  //total line count
			char endbuf[MAXLINE];                     //send cliend when (".")

			again: while ((n = read(connfd, buf, MAXLINE)) > 0) {

				if (strcmp(buf, stop) == 0)
				{
					Writen(connfd, endbuf, strlen(endbuf));
					break;
				}
				else
				{
					memset(&endbuf, 0, sizeof(endbuf));
					sprintf(byteCount, "%d", n - 1); //number of byte

					totalbyte = totalbyte + n - 1;

					token = strtok(buf, seps);

					while (token != NULL) {
						token = strtok(NULL, seps);
						intCount++;
					}

					totalword = totalword + intCount;

					sprintf(wordCount, "%d", intCount); //number of word
					strcat(sendbuf, "ByteCount is ");
					strcat(sendbuf, byteCount);
					strcat(sendbuf, " WordCount is ");
					strcat(sendbuf, wordCount);
					strcat(sendbuf, "\n");
					intCount = 0;
					Writen(connfd, sendbuf, strlen(sendbuf));

					totalline++;

					strcat(endbuf,"TotalByte is ");
					sprintf(totalByte,"%d",totalbyte);
					strcat(endbuf,totalByte);

					strcat(endbuf," TotablWord is ");
					sprintf(totalWord,"%d",totalword);
					strcat(endbuf,totalWord);

					strcat(endbuf," TotalLine is ");
					sprintf(totalLine,"%d",totalline);
					strcat(endbuf,totalLine);
					strcat(endbuf, "\n");

					memset(&buf, 0, sizeof(buf));
					memset(&sendbuf, 0, sizeof(sendbuf));
				}
			}

			if (n < 0 && errno == EINTR)
				goto again;

			else if (n < 0)
				err_sys("str_echo: read error");

			//str_echo(connfd);	/* process the request */
			exit(0);
		}
		Close(connfd); /* parent closes connected socket */
	}
}

